const {SettingModel} = require('../../models')

const koaTool = require('../../commons/koa_tool')


class SettingController {
    create = async (ctx) => {

        let [checkError, input] = koaTool.checkInput(ctx, ['type', 'content'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }
        let {type,content} = input;
        let isType = await SettingModel.findOne({type})
        if (isType) {
            return koaTool.error(ctx, "类型已存在")
        }

        let newData = Object.assign({}, input, {})
        let detail = await SettingModel.create(newData);
        
        koaTool.succ(ctx, {
            detail
        })
    }

    detail = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['id'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }

        const detail = await SettingModel.findById(input.id)
        koaTool.succ(ctx, {
            detail
        })
    }

    update = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['id'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }
        let detail = await SettingModel.findById(input.id)
        if (!detail) {
            return koaTool.succ(ctx, {}, 405, '数据不存在')
        }
        const {
            type, content
        } = input
        if (type) {
            detail.type = type
        }
        if (content != undefined) {
            detail.content = content;
        }
        await detail.save();
        koaTool.succ(ctx, {
            detail
        })

    }

    delete = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['id'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }
        await SettingModel.findByIdAndRemove(input.id)
        koaTool.succ(ctx, {
            msg: '成功'
        })
    }

    list = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['page', 'limit'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }
        let {page, limit} = input

        limit = Number(limit)
        page = page || 0
        page = page - 1
        let query = {
        };
        let total = await SettingModel.countDocuments(query)
        let list = await SettingModel.find(query).sort([['_id', -1]]).skip(page * limit).limit(limit)
        koaTool.succ(ctx, {
            list,
            total
        })
    }

    getTypeList = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, []);
        const {SETTING_TYPE_LIST} = require('../../commons/const.js');
        koaTool.succ(ctx, {
            list:SETTING_TYPE_LIST
        })
    }

    getTypeMap = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, []);
        const {SETTING_TYPE_MAP} = require('../../commons/const.js');
        koaTool.succ(ctx, {
            map:SETTING_TYPE_MAP
        })
    }


}

module.exports = new SettingController()
